<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 属性控制器
 * @author huajie <banhuajie@163.com>
 */
class GoodsController extends AdminController {

    /**
     * 属性列表
     * @author huajie <banhuajie@163.com>
     */
    public function index(){
//         $model_id = I('get.model_id');
//         /* 查询条件初始化 */
//         $map['model_id']    =   $model_id;

//         $list = $this->lists('Attribute', $map);
//         int_to_string($list);

//         // 记录当前列表页的cookie
//         Cookie('__forward__',       $_SERVER['REQUEST_URI']);
//         $this->assign('_list',      $list);
//         $this->assign('model_id',   $model_id);
//         $this->meta_title = '属性列表';
//         $this->display();
        
//         file_put_contents("g:/mylog.log","1111"."\r\n",FILE_APPEND);
        
        $Goods = M('Goods');
        $Goodsrule = M('Goodsrule');
        $list =  $Goods->select();
        $rulelist =  $Goodsrule->select();
        file_put_contents("g:/mylog.log",$list."\r\n",FILE_APPEND);
        //         // 记录当前列表页的cookie
        Cookie('__forward__',       $_SERVER['REQUEST_URI']);
        $this->assign('list', $list);
        $this->assign('rulelist', $rulelist);
        //          $this->assign('model_id',   $model_id);
        //          $this->meta_title = '属性列表';
        $this->display();
    }

    
    
    /**
     * 分类列表
     * @author hexin<332050842@qq.com>
     */
    public function classlist(){
    	//         $pid = i('get.pid', 0);
    	//         /* 获取频道列表 */
    	//         $map  = array('status' => array('gt', -1), 'pid'=>$pid);
    	//         $list = M('Channel')->where($map)->order('sort asc,id asc')->select();
    	//         $this->assign('list', $list);
    	//         $this->assign('pid', $pid);
    	
    	$list = M('goodscat')->order('sort asc,id asc')->select();
    	$this->assign('aadd', "ccss");
    	$this->assign('list', $list);
    	
    	$this->meta_title = '商家管理';
    	$this->display();
    }
    
    /**
     * 添加商家
     * @author hexin<332050842@qq.com>
     */
    public function goodsadd(){
   
        //获取商家信息列表
        $shoplist      =   M('Goodshops')->where('state=1')->field('id,shopname')->order('sort')->select();
        //获取分类信息
        $cate      =   M('Goodscat')->where('state=1')->field('id,cat_name')->order('sort')->select();
       // file_put_contents("g:/mylog.log","tttt"."\r\n",FILE_APPEND);
        //获取分类信息
        $this->assign('catlist', $cate);
        $this->assign('shoplist', $shoplist);
    	$this->display('goodsadd');
    	
   	 
    }
    
    
    /**
     * 添加商家
     * @author hexin<332050842@qq.com>
     */
    public function goodsedit(){
         //在else部分查找记录 在  if(IS_POST) 部分修改记录
     if(IS_POST){
            $GoodsCate = D('goodscat');            
            $data = $GoodsCate->create();
            $sum=$GoodsCate->save($data);
            file_put_contents("g:/mylog.log","===".$sum."\r\n",FILE_APPEND);
            if($sum){

    				$this->success('修改成功', U('classlist'));
    				//记录行为
    				action_log('update_config', 'goodscat', $id, UID);
                } else {
                				$this->error('新增失败');
                }
            }else{
                //获取商家信息列表
                $shoplist      =   M('Goodshops')->where('state=1')->field('id,shopname')->order('sort')->select();
                //获取分类信息
                $cate      =   M('Goodscat')->where('state=1')->field('id,cat_name')->order('sort')->select();
                // file_put_contents("g:/mylog.log","tttt"."\r\n",FILE_APPEND);
                //获取分类信息
              
            $good_id   =   I('get.good_id');
          //  file_put_contents("g:/mylog.log","=cat_id==".$cat_id."\r\n",FILE_APPEND);
            
            $goodone      =   M('Goods')->find($good_id);
            $goodrules= M('Goodsrule')->where(array('good_id' =>$good_id ))->select();
           file_put_contents("g:/mylog.log","=1111==".$goodone['good_name']."\r\n",FILE_APPEND);
//             $goodcatone=M();
           $this->assign('catlist', $cate);
           $this->assign('shoplist', $shoplist);
           $this->assign('rulelist',$goodrules);
             $this->assign('info',$goodone);
//             $this->meta_title = '新增导航';
             $this->display();
        }
         
         
    }
    
    //
    //删除规格
    public function goodsruledel(){
        
        $rule_id   =   I('post.rule_id');
        $map = array('id' => $rule_id );      
        if(M('Goodsrule')->where($map)->delete()){
            session('ADMIN_MENU_LIST',null);
            //记录行为
            action_log('update_menu', 'Menu', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    
    }
    //修改规格ruledel
    public function ruleedit(){
        if(IS_POST){
            file_put_contents("g:/mylog.log","=222RRR=="."\r\n",FILE_APPEND);
                $Goodsrule = D('goodsrule');
                $data = $Goodsrule->create();
                $data['id']=   I('post.rule_id');
                //$rule_id   =   I('post.rule_id');
               //file_put_contents("g:/mylog.log","=222RRR=="."\r\n",FILE_APPEND);
               // $map = array('id' => $rule_id );
                file_put_contents("g:/mylog.log",$data['rule_name']."=333RRR==".$data['id']."\r\n",FILE_APPEND);
                if(M('goodsrule')->save($data)){
                    file_put_contents("g:/mylog.log","=cgRRR=="."\r\n",FILE_APPEND);
                    session('ADMIN_MENU_LIST',null);
                    //记录行为
                   // action_log('update_menu', 'Menu', $id, UID);
                    $this->success('修改成功');
                   // $this->redirect('New/category', array('cate_id' => 2), 5, '页面跳转中...');
                } else {
                    file_put_contents("g:/mylog.log","=sbRRR=="."\r\n",FILE_APPEND);
                    $this->error('修改失败！');
                }
        }else{
            
            file_put_contents("g:/mylog.log","=1111RRR=="."\r\n",FILE_APPEND);
          $rule_id   =   I('get.rule_id');
          //  file_put_contents("g:/mylog.log","=cat_id==".$cat_id."\r\n",FILE_APPEND);
            
            $ruleone      =   M('goodsrule')->find($rule_id);
        //    $goodrules= M('Goodsrule')->where(array('good_id' =>$good_id ))->select();
       //   file_put_contents("g:/mylog.log","=1111==".$goodone['good_name']."\r\n",FILE_APPEND);
//             $goodcatone=M();
//            $this->assign('catlist', $cate);
//            $this->assign('shoplist', $shoplist);
//            $this->assign('rulelist',$goodrules);
             $this->assign('ruleinfo',$ruleone);
//             $this->meta_title = '新增导航';
             $this->display();
        }
    
    }
    
    
    /**
     * 增加商品
     * @author huajie <banhuajie@163.com>
     */
    public function insertone(){
       
        //file_put_contents("g:/mylog.log","aa123"."\r\n",FILE_APPEND);
       // $good_name=$_POST['good_name'];
        
        //       /* /*   //解析规格  并出入数据库
        //         $ruleall=$_POST['ruleall'];
        //         file_put_contents("g:/mylog.log",$ruleall."\r\n",FILE_APPEND);
        //         $de_json = json_decode($ruleall,TRUE);
        //         $count_json = count($de_json);
                
        //        //将规格循环插入数据库中
        //        for ($i = 0; $i < $count_json; $i++)
        //        {
        //     file_put_contents("g:/mylog.log","==ddd123=".$de_json[$i]['ruleprice']."\r\n",FILE_APPEND);
        //     $Goodsrule = D('Goodsrule');
        //     $data['good_id'] = 11;
        //     $data['rule_name'] = $de_json[$i]['rulename'];
        //     $data['rule_price'] = $de_json[$i]['ruleprice'];
        //     $data['stock'] = $de_json[$i]['rulestock'];
        //     $Goodsrule->add($data);
            
        //        } */ */
        $Goods=D('Goods');
        $data = $Goods->create();
        file_put_contents("g:/mylog.log","=ggg==".$data['cat_id']."\r\n",FILE_APPEND);
        
        if($data){
            $id = $Goods->add();
            if($id){        
            				$this->success('新增成功', U('classlist'));
            				//记录行为
            				action_log('update_config', 'goodscat', $id, UID);
            } else {
            				$this->error('新增失败');
            }
        } else {
            $this->error($GoodsCate->getError());
        }

   
    }
    /**
     * 添加类别
     * @author hexin<332050842@qq.com>
     */
    public function classadd(){
        //file_put_contents("g:/mylog.log","=1111=="."\r\n",FILE_APPEND);
    	if(IS_POST){
    		$GoodsCate = D('goodscat');
	
    		$data = $GoodsCate->create();
    		file_put_contents("g:/mylog.log","===".$data."\r\n",FILE_APPEND);
    		if($data){
    			$id = $GoodsCate->add();
    			if($id){
//     				//插入到目录中
//     				$Menu = D('Menu');
//     				$Mdata['title'] = $data['cat_name'];
//     				$Mdata['pid'] = '124';
//     				$Mdata['sort'] = $data['sort'];
//     				$Mdata['url'] = 'Goods/classlist/'.$id.'.html';
//     				$Mdata['group'] = '商品管理';
//     				$Menu->add($Mdata);
    				
    				$this->success('新增成功', U('classlist'));
    				//记录行为
    				action_log('update_config', 'goodscat', $id, UID);
    			} else {
    				$this->error('新增失败');
    			}
    		} else {
    			$this->error($GoodsCate->getError());
    		}
    	} else {

    		$this->assign('info',null);
    		$this->meta_title = '新增导航';
    		$this->display('classedit');
    	}
    	
    }
    
    
    /**
     * 修改类别类别
     * @author hexin<332050842@qq.com>
     */
    public function classedit($id=0){
        //file_put_contents("g:/mylog.log","=1111=="."\r\n",FILE_APPEND);
        //看下是不是post  不是表示是显示数据  显示的时候带上id
        if(IS_POST){
            $GoodsCate = D('goodscat');            
            $data = $GoodsCate->create();
            $sum=$GoodsCate->save($data);
            file_put_contents("g:/mylog.log","===".$sum."\r\n",FILE_APPEND);
            if($sum){

    				$this->success('修改成功', U('classlist'));
    				//记录行为
    				action_log('update_config', 'goodscat', $id, UID);
                } else {
                				$this->error('新增失败');
                }
            }else{
            $cat_id   =   I('get.cat_id');
          //  file_put_contents("g:/mylog.log","=cat_id==".$cat_id."\r\n",FILE_APPEND);
            
            $goodcatone      =   M('Goodscat')->field('id,cat_name,sort')->find($cat_id);
            
        //    file_put_contents("g:/mylog.log","=1111==".$goodcatone['cat_name']."\r\n",FILE_APPEND);
//             $goodcatone=M();
             $this->assign('info',$goodcatone);
//             $this->meta_title = '新增导航';
             $this->display();
        }
 
         
    }
    
    /**
     * 删除类别
     * @author hexin 
     */
    public function del(){
        $cat_id   =   I('get.id');
        $map = array('id' => array('in', $cat_id) );
        $goodmap = array('cat_id' => array('in', $cat_id) );
        if ( M("Goods")->where($goodmap)->find()){
            $this->error('删除失败！该分类下还有商品');
        }else {
       
        if(M('Goodscat')->where($map)->delete()){
            session('ADMIN_MENU_LIST',null);
            //记录行为
            action_log('update_menu', 'Menu', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }
    }
    
    /**
     * 菜单排序
     * @author huajie <banhuajie@163.com>
     */
    public function sort(){
        //传入指
        $list = M('Goodscat')->order('sort asc,id asc')->select();
        $this->assign('list', $list);
        $this->meta_title = '菜单排序';
        $this->display();
        if (IS_POST){
            $ids = I('post.ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key=>$value){
               // file_put_contents("g:/mylog.log","=567val==".$value."======key===".$key."\r\n",FILE_APPEND);
                $res = M('Goodscat')->where(array('id'=>$value))->setField('sort', $key+1);
                file_put_contents("g:/mylog.log","=888999==".$res."\r\n",FILE_APPEND);
            }
            file_put_contents("g:/mylog.log","=进到这里了=="."\r\n",FILE_APPEND);
          
            if($res !== false){
                session('ADMIN_MENU_LIST',null);
                file_put_contents("g:/mylog.log","=执行了11111=="."\r\n",FILE_APPEND);
                //$this->success('排序成功！',U('classlist'));
                $this->success('排序成功！');
                file_put_contents("g:/mylog.log","=排序成功="."\r\n",FILE_APPEND);
               // $this->success('排序成功', U('classlist'));
            }else{
                file_put_contents("g:/mylog.log","=执行了2222=="."\r\n",FILE_APPEND);
                $this->error('排序失败！');
            }
        }
       
    }
    
/**
     * 修改类别
     * @author hexin
     */
    public function edit($id = 0){
        if(IS_POST){
            $Menu = D('Menu');
            $data = $Menu->create();
            if($data){
                if($Menu->save()!== false){
                    session('ADMIN_MENU_LIST',null);
                    //记录行为
                    action_log('update_menu', 'Menu', $data['id'], UID);
                    $this->success('更新成功', Cookie('__forward__'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($Menu->getError());
            }
        } else {
            
            $this->display();
        }
    }
    /**
     * 新增页面初始化
     * @author huajie <banhuajie@163.com>
     */
    public function add(){
        $model_id   =   I('get.model_id');
        $model      =   M('Model')->field('title,name,field_group')->find($model_id);
        $this->assign('model',$model);
        $this->assign('info', array('model_id'=>$model_id));
        $this->meta_title = '新增属性';
        $this->display('edit');
    }

   


   
}
